<template>
  <div class="bg">
    <div>
      <el-carousel :interval="4000" type="card" height="200px">
        <el-carousel-item v-for="item in 6" :key="item">
          <h3 class="medium">{{ item }}</h3>
        </el-carousel-item>
      </el-carousel>
    </div>
    <div class="container">
      <div class="header">
        <h2 style="color: white; text-align: center; line-height: 40px;border:1px;  border-radius: 10%;">
          {{index}}
        </h2>
      </div>
      <div class="content">
        <ul>
          <li
            :class="[record[index].sort == 1 ? 'classaa' : 'classbb']"
            v-for="(item, index) in record"
            :key="item.content"
          >
            <div v-if="record[index].sort == 0">
              
              <span>
                <img
                  class="touxiang1"
                  v-show="play"
                  src="../../assets/404_images/self.png"
                />
              </span>

              <span class="span1"> {{ record[index].content }} </span>

            </div>

            <div v-if="record[index].sort == 1">
              <span >
                <img
                class="touxiang2"
                v-show="play"
                src="../../assets/404_images/nan.png"
              />
              </span>
              <span class="span2" >
                {{ record[index].content }}
              </span>
            </div>
          </li>
        </ul>

        <!-- <li  > -->

        <!-- :class="[record[index].sort>0 ? 'classaa' : 'classbb']" -->

        <!-- </li>
        </ul> -->
      </div>

      <div class="typing">
        
        <el-input v-model="my_content"></el-input>
        <el-button @click="add" style="float: left">
          {{ bt_change }}
        </el-button>
        <el-button v-show="s_play" @click="send" style="float: right"> 发送 </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import Qs from'qs'
import axios from 'axios'

// Vue.forceUpdate();

import { mapActions, mapMutations, mapState } from "vuex";
export default {
 
  computed: {
    ...mapState("console1", ["data"]),
  },
  data() {
    return {
      b:0,
        index:'等待为您安排患者',
        timer:'',
      play: false,
      s_play:false,
      my_content: "",
      bt_change: "开始咨询",
      data_obtain: { data: "" },
      record: [{ sort: "", content: "" ,title:0}],
      record_content: { sort: "1", content: "-1",title:0 },
      // test:[0,1,2,3],
      obj: { id: 32 },

      a: 0,
      console_list1: {
        title: "console1",
        content: "",
        categoryId: 5,
        id: "159",
      },
      console_list: { title: "console", content: "", categoryId: 5, id: "81" },
    };
  },
  mounted() {},

  created() {
    //  this.response =
    //   this.res= this.text_obtain(this.obj).then(()=>{console.log('123165');
    //  });
    //   console.log(this.res);
    // this.text_obtain(this.obj);
    // this.res = this.data;
  },
  // this.res=this.res.state.content

  methods: {
    
    ...mapMutations("console1", ["set_data"]),
    ...mapActions("console1", ["text_obtain", "saveOrUpdate1"]),
    to_login() {
      axios({
        url: "http://39.102.37.84:8001/user/login",
        method: "post",
        data: this.form_login,
      }).then((res) => {
        // 1. 关闭模态框
        this.dialog_login = false;
        // 2. 切换页面
        this.token = res.data.data.token;
        // 3. 将token设置到浏览器
        localStorage.setItem("token", res.data.data.token);
        // 4. 跳转页面
        // this.$router.push({
        //   path:'/article'
        // })
      });
    },
    publish(params) {
      axios({
        url: "http://39.102.37.84:8001/article/saveOrUpdate",
        method: "post",
        data: Qs.stringify(params),
        headers:{
            'Authorization':localStorage.getItem('token')
        }
      }).then(() => {
        
       
       
      });
    },
    send() {
     
      // console.log(this.record[0],this.a,this.record[this.a-1]);
     
      let con = { sort: "", content: "" };
      con.content = this.my_content;
      con.sort = 0;
      con.title=this.b;
      this.record[this.a] = con;
      
      let params={id:11,content:'',title:'console',content:'',categoryId:5}
      params.content=con.content
      params.title=con.title

    //   params=Qs.stringify(params)
    //   this.to_login();
    console.log(params);
       this.publish(params);
       this.a++;
       this.b++;
    // this.saveOrUpdate1(params)
      this.my_content = "";
      
      this.$forceUpdate();
    },
    methodThatForcesUpdate() {
      // ...
      this.$forceUpdate();
      // ...
    },

    add() {
      // this.record[2] = 15;
      // console.log(this.record);
      if (this.bt_change == "开始咨询") {
        this.bt_change = "结束咨询";
        this.interval();
        this.s_play=true;
        this.index='已为您分配患者'
         let params={id:11,title:-1,categoryId:5,content:'医生正在火速赶到，请您先输入咨询问题'}
         this.publish(params)
        this.$forceUpdate();
        this.play = true;
      } else {
              clearInterval(this.timer);
        this.bt_change = "开始咨询";
          let params={id:11,title:this.b-1,categoryId:5,content:'医生正在火速赶到，请您先输入咨询问题'}
         this.publish(params)
         window.sessionStorage.clear()
        this.s_play=false
         this.$forceUpdate();
        
        // this.delete_console()
      }
    },
    interval() {
      console.log(this.record[0]);
      this.timer=setInterval(() => {
        let res = "";
        this.text_obtain(this.obj).then(() => {
          this.res = this.data.data.content;

          // console.log(this.record[0]);

          if (this.record_content.title != this.data.data.title) {
            sessionStorage.setItem('messagea'+this.a,this.res)
            sessionStorage.setItem('messagea1'+this.a,this.data.data.title)
            let con1={sort:'',content:'',title:0};
            con1.sort=1;
            con1.content=sessionStorage.getItem('messagea'+this.a)
            con1.title=sessionStorage.getItem('messagea1'+this.a)
            this.record[this.a]=con1
            // this.record[this.a].sort=1;
            // this.record[this.a].content=localStorage.getItem('messagea')
            this.record_content.content = this.res;
             this.record_content.title = this.data.data.title;
            this.a++;
            // this.record_content.sort = 1;
            // this.record[this.a] = this.record_content;
            // console.log(this.record[this.a].content);
            
          }
          this.$forceUpdate();
        });
      }, 5000);
    },

    
  },
};
</script>


<style scoped>
button{
  height: 30px;
  color: rgb(112, 175, 194);
}
 
.container{
    height: 621.99px;
    background-image: url(../../assets/404_images/doctor1.jpg);
}

span{

  width: 300px;
  line-height: 30px;
}
.classaa {
  padding: 10px;
  float: left;
}
.span1 {
  margin-top: 10px;
  float: right;
  background-color: rgb(36, 176, 211);
  border-radius: 5px;
}
.span2 {
  margin-top: 10px;
  float: right;
}
.classbb {
  float: right;
}
* {
  margin: 0;
  padding: 0;
}
.touxiang1 {
  width: 50px;
  height: 50px;
  float: right;
}

.touxiang2 {
  width: 50px;
  height: 50px;
  float: left;
}
li {
  list-style: none;
}
.typing {
  margin-left: 420px;
  width: 640px;
}
.content {
  margin-left: 420px;

  overflow: scroll;
  background-color: #eeeeee;
  width: 640px;
  height: 500px;
}

.header {
    border-radius:5%;
  margin-left: 420px;

  width: 640px;
  height: 40px;
  background-color: #00bcd4;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>